package com.sy.service.impl;

import com.sy.entity.Admin;
import com.sy.entity.Student;
import com.sy.entity.Teacher;
import com.sy.entity.User;
import com.sy.exception.BusinessException;
import com.sy.manage.SmsService;
import com.sy.mapper.AdminMapper;
import com.sy.mapper.StudentMapper;
import com.sy.mapper.TeacherMapper;
import com.sy.service.UserService;
import com.sy.util.RandomUtils;
import com.sy.util.RedisOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.sy.constant.BusinessConstant.*;
import static com.sy.constant.BusinessConstant.VALIDATE_CODE_EXPIRE_SECONDS;
import static com.sy.constant.RegexConstant.NUM_REGEX;
import static com.sy.constant.RegexConstant.PHONE_REGEX;

/**
 * @Author:cxl
 * @Date: 2021/10/12 13:00
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private RedisOperations redisOperations;
    @Autowired
    private SmsService smsService;

    @Override
    public boolean isUserNameExists(String userName) {
        if (userName.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "登陆用户不能为空");
        }
        if (!userName.matches(NUM_REGEX)){
            System.out.println();
            boolean adminName = Objects.isNull(adminMapper.getAdminByAdminName(userName));
            if(adminName){
                throw new BusinessException(HttpStatus.BAD_REQUEST, "该管理员不存在");
            }
        }else{
            System.out.println(userName);
            boolean studentBySno = Objects.isNull(studentMapper.getStudentBySno(Integer.valueOf(userName)));
            boolean teacherByTno = Objects.isNull(teacherMapper.getTeacherByTno(Integer.valueOf(userName)));
            if (studentBySno && teacherByTno){
                throw new BusinessException(HttpStatus.BAD_REQUEST, "该用户不存在");
            }
        }
        return true;
    }

    @Override
    public User login(User user) {
        String name = user.getName();
        String pwd = user.getPwd();
        Integer type = user.getType();
        if(name.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "登陆用户不能为空");
        }
        if(pwd.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "登陆用户密码不能为空");
        }
        if (String.valueOf(type).isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "登陆用户类型不能为空");
        }else {
            if (type.equals(ADMIN_TYPE)){
                if(Objects.isNull(adminMapper.loginAdmin(user))){
                    throw new BusinessException(HttpStatus.BAD_REQUEST, "您的密码错误");
                }else{
                    Admin admin = adminMapper.loginAdmin(user);
                    user.setTrueNAME(admin.getName());
                }

            }else if(type.equals(TEACHER_TYPE)){
                Teacher teacher = teacherMapper.loginTeacher(user);
               if(Objects.isNull(teacher)){
                   throw new BusinessException(HttpStatus.BAD_REQUEST, "您的密码错误");
               }else {
                   if(teacher.getType().equals(0)){
                       throw new BusinessException(HttpStatus.BAD_REQUEST, "该教师已离职");
                   }
                   user.setTrueNAME(teacher.getName());

               }
            }else {
                Student student = studentMapper.loginStudent(user);
                if(Objects.isNull(student)){
                    throw new BusinessException(HttpStatus.BAD_REQUEST, "您的密码错误");
                }else {
                    if(student.getType().equals(0)){
                        throw new BusinessException(HttpStatus.BAD_REQUEST, "该学生已离校");
                    }
                    user.setTrueNAME(student.getName());
                }
            }
        }
        return user;
    }

    @Override
    public User getUserTrueName(User user) {
        String name = user.getName();
        Integer type = user.getType();
        if(name.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "登陆用户不能为空");
        }
        if (!type.equals(STUDENT_TYPE) ||!type.equals(TEACHER_TYPE) ||!type.equals(ADMIN_TYPE)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "登陆用户类型异常");
        }
        if(type.equals(ADMIN_TYPE)){
            return user;
        } else if (type.equals(TEACHER_TYPE)){
            Teacher teacherByTno = teacherMapper.getTeacherByTno(Integer.valueOf(name));
            if (Objects.isNull(teacherByTno)){
                throw new BusinessException(HttpStatus.BAD_REQUEST, "该教师不存在");
            }
            if(teacherByTno.getType().equals(USER_NO_OK_TYPE)){
                throw new BusinessException(HttpStatus.BAD_REQUEST, "该教师以离职");
            }

        }
        return null;
    }

    @Override
    public boolean sendValidateCode(String phone) {
        //手机号是否合法
        if (!phone.matches(PHONE_REGEX)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "手机号格式不合法");
        }

        String code = RandomUtils.generateValidateCode(VALIDATE_CODE_LENGTH);
        boolean smsSuccess = smsService.sendValidateCode(phone, code);
        if (!smsSuccess) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR, "短信发送失败！");
        } else {
            String key = REDIS_VALIDATE_CODE_PREFIX + phone;
            redisOperations.set(key, code);
            redisOperations.expire(key, VALIDATE_CODE_EXPIRE_SECONDS);
        }
        return true;
    }

}

